home *** CD-ROM | disk | FTP | other *** search
/ Disc to the Future 2 / Disc to the Future Part II Programmer's Reference (Wayzata Technology)(6013)(1992).bin / MAC / THINKC / TCL1 / MIDI_MAN / CMIDIPOR.C < prev    next >
Text File  |  1992-03-07  |  6KB  |  177 lines

  1. //--- CMIDIPort.c ----------------------------------------------------------------------
  2. // Copyright ⌐ Paul Ferguson, 1990, 1991, 1992.  All rights reserved.
  3. //
  4. // Superclass:  CObject
  5. // Subclasses:  CDataPort CTimePort
  6. //
  7. // Description:
  8. //    CMIDIPort.c defines a MIDI Manager port object.  CMIDIPort is an abstract type,
  9. //    containing variables and methods common to all three port types.
  10. //
  11. //    For use with THINK C 5.0, the accompanying THINK Class Library (TCL), and MIDI
  12. //    Manager 2.0. Refer to the accompanying Microsoft Word document for complete
  13. //    details about MIDI Manager objects.
  14. //
  15. //    If you have comments or questions about this code, you can reach me on
  16. //    CompuServe at 70441,3055.
  17. //
  18. //--------------------------------------------------------------------------------------
  19. //---- NOTE --- NOTE --- NOTE --- NOTE --- NOTE --- NOTE --- NOTE --- NOTE --- NOTE ----
  20. //--------------------------------------------------------------------------------------
  21. //    If you are not familiar with programming the Apple MIDI Manager, refer to the
  22. //    "MIDI Management Tools" Version 2.0, available from APDA.  You MUST have the
  23. //    software (MIDI.H and the library) from this package in order to use these objects.
  24. //    It will not work without this.
  25. //--------------------------------------------------------------------------------------
  26. //    REVISION HISTORY:
  27. //        August ??, 1990            - Original release (1.0).
  28. //        November 5, 1990        - Added checks for midiMgrVer to most methods.
  29. //        August 1991                - updated for THINK C 5.0 as version 2.0
  30. //--------------------------------------------------------------------------------------
  31.  
  32. #include "CMIDIPort.h"                    // This code's header file
  33.  
  34. extern    OSType    gSignature;                // Used to register client
  35. extern CMIDIClient * gMIDIClient;
  36.  
  37. //--- CMIDIPort methods ----------------------------------------------
  38. // Methods for a MIDI Manager  port.  This is an abstract object.
  39. // Superclass        CObject
  40. // Subclasses        CDataPort, CTimePort
  41. //--------------------------------------------------------------------
  42.  
  43.  
  44. OSErr CMIDIPort::IMIDIPort(MIDIPortParamsPtr portParams, short bufSize)
  45. {
  46.     short            theRefNum;
  47.     unsigned char *    theLen;
  48.  
  49.     if ((gMIDIClient == 0) ||
  50.         ((itsVersion = gMIDIClient->GetShortVerNum()) == 0))
  51.     {
  52.         itsVersion = itsPortID = itsRefNum = 0;
  53.         return (ErrNoMIDI);
  54.     }
  55.  
  56. // Otherwise, call MIDIAddPort
  57.  
  58.     theLen = &(portParams->name[0]);        // Truncate name if necessary
  59.     if (*theLen > midiMaxNameLen) *theLen = midiMaxNameLen;
  60.  
  61.     itsPortID = portParams->portID;        // Save our port ID in the object
  62.     itsResult = MIDIAddPort(gSignature, bufSize, &theRefNum, portParams);
  63.     itsRefNum = theRefNum;                // Save the port reference number
  64.     return (itsResult);
  65. }
  66.  
  67. //--- CMIDIPort::Dispose ----------------------------------------------------------
  68. // Dispose of this MIDI port.  Note that we do NOT call MIDIRemovePort() here.
  69. // This has been seen to cause problems with the MIDI Manager.
  70. //
  71. // Normally, this isn't a problem, since you typically would only call this when
  72. // you are quitting.  If you specifically need to call MIDIRemovePort(), then you
  73. // can override this method.
  74. //---------------------------------------------------------------------------------
  75.  
  76. void CMIDIPort::Dispose(void)
  77. {
  78.     inherited::Dispose();
  79. }
  80.  
  81. //--- CMIDIPort::SavePatches -------------------------------------------------
  82. // Saves the current port connections in a specified resource.
  83. //----------------------------------------------------------------------------
  84.  
  85. OSErr CMIDIPort::SavePatches(ResType theResType, short theResID)
  86. {
  87.     Str255                theName;
  88.     Handle                h;
  89.     MIDIPortInfoHdl        portInfo;
  90.  
  91.     if ( ! itsVersion )
  92.         return (ErrNoMIDI);
  93.  
  94.     if (itsResult != noErr)                // May have been a virtual connection,
  95.         return(itsResult);                // so don't do anything.
  96.  
  97.     GetPortName(theName);
  98.     h = GetResource(theResType, theResID);    // Delete resource if it exists
  99.     if (h)
  100.     {
  101.         RmveResource(h);
  102.         DisposHandle(h);
  103.         UpdateResFile(CurResFile());
  104.     }
  105.     portInfo = (MIDIPortInfoHdl) MIDIGetPortInfo(gSignature, itsPortID);
  106.     if (portInfo)
  107.     {
  108.         if ((**portInfo).numConnects > 0)    // No connection, no record
  109.         {
  110.             AddResource(portInfo, theResType, theResID, theName);
  111.             WriteResource(portInfo);
  112.             UpdateResFile(CurResFile());
  113.         }
  114.         ReleaseResource(portInfo);
  115.     }
  116.     return (noErr);
  117. }
  118.  
  119. //--- Misc, trivial methods -------------------------------------------
  120. // These aren't individually commented because frankly, I didn╒t feel
  121. // like it, nor do they really need any comments besides this one.
  122. //---------------------------------------------------------------------
  123.  
  124. short CMIDIPort::GetRefNum(void)
  125. {
  126.     return (itsRefNum);
  127. }
  128.  
  129. long CMIDIPort::GetRefCon(void)
  130. {
  131.     return (itsVersion ? MIDIGetRefCon(itsRefNum) : 0);
  132. }
  133.  
  134. void CMIDIPort::SetRefCon(long theRefCon)
  135. {
  136.     if (itsVersion)
  137.         MIDISetRefCon(itsRefNum, theRefCon);
  138. }
  139.  
  140. void CMIDIPort::GetPortName(StringPtr theName)
  141. {
  142.     if (itsVersion)
  143.         MIDIGetPortName(gSignature, itsPortID, theName);
  144. }
  145.  
  146. void CMIDIPort::SetPortName(StringPtr theName)
  147. {
  148.     if (itsVersion)
  149.     {
  150.         if (theName[0] > midiMaxNameLen)
  151.             theName[0] = midiMaxNameLen;
  152.         MIDISetPortName(gSignature, itsPortID, theName);
  153.     }
  154. }
  155.  
  156. MIDIPortInfoHdl    CMIDIPort::GetPortInfo(void)
  157. {
  158.     return(itsVersion ? MIDIGetPortInfo(gSignature, itsPortID) : 0);
  159. }
  160.  
  161. void CMIDIPort::SetConnectionProc(ProcPtr theConnectProc, long theRefCon)
  162. {
  163.     if (itsVersion >= 0x200)        // Version 2.0 or later
  164.         MIDISetConnectionProc(itsRefNum, theConnectProc, theRefCon);
  165. }
  166.  
  167.  
  168. void CMIDIPort::GetConnectionProc(ProcPtr * theConnectProc, long * theRefCon)
  169. {
  170.     if (itsVersion >= 0x200)        // Version 2.0 or later
  171.         MIDIGetConnectionProc(itsRefNum, theConnectProc, theRefCon);
  172. }
  173.  
  174.  
  175.  
  176. // end of CMIDIPort.c
  177.